// components/swiper/index.js
const computedBehavior = require('miniprogram-computed').behavior
import config from '../../config/index'

Component({
  behaviors: [computedBehavior],

  /**
   * 组件的属性列表
   */
  properties: {
    swipers: { // 轮播图数据
      type: Array,
      value: []
    }
  },

  watch: {
		swipers(val) {
      if (Array.isArray(val) && val.length) {
        if (val[0].interval != null) {
          this.setData({
            interval: val[0].interval.seconds,
          })
        }
      }
    }
	},

  /**
   * 组件的初始数据
   */
  data: {
    // 切换间隔
    interval: config.swiperInterval,
    current: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 轮播图切换
    onSwiperChange(event) {
      const { current } = event.detail
      const interval = this.data.swipers[current].interval

      this.setData({
        interval: interval != null ? interval.seconds : config.swiperInterval,
        current,
      })
    },

    // 打开链接
    _onClick(event) {
			const { item } = event.currentTarget.dataset
      this.triggerEvent('btnpress', item, {
        bubbles: true, 
        composed: true
      })
		}
  }
})
